package com.menu.web.controller.reception;

import com.menu.common.annotation.Log;
import com.menu.common.core.controller.BaseController;
import com.menu.common.core.domain.AjaxResult;
import com.menu.common.core.page.TableDataInfo;
import com.menu.common.enums.BusinessType;
import com.menu.common.enums.DataSourceType;
import com.menu.common.utils.SecurityUtils;
import com.menu.common.utils.poi.ExcelUtil;
import com.menu.framework.datasource.DynamicDataSourceContextHolder;
import com.menu.system.domain.MenuUserRole;
import com.menu.system.domain.ReceptionUser;
import com.menu.system.service.IMenuUserRoleService;
import com.menu.system.service.IreceptionRoleService;
import com.menu.system.service.IreceptionUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息Controller
 *
 * @author ruoyi
 * @date 2024-06-06
 */
@RestController
@RequestMapping("/system/receptionUser")
public class receptionUserController extends BaseController {
    @Autowired
    private IreceptionUserService receptionUserService;
    @Autowired
    private IMenuUserRoleService requestUserRoleService;
    @Autowired
    private IreceptionRoleService receptionRoleService;

    /**
     * 查询用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:receptionUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReceptionUser receptionUser) {
        startPage();
        List<ReceptionUser> list = receptionUserService.selectreceptionUserList(receptionUser);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:receptionUser:export')")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReceptionUser receptionUser) {
        List<ReceptionUser> list = receptionUserService.selectreceptionUserList(receptionUser);
        ExcelUtil<ReceptionUser> util = new ExcelUtil<ReceptionUser>(ReceptionUser.class);
        util.exportExcel(response, list, "用户信息数据");
    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:receptionUser:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put(AjaxResult.DATA_TAG, receptionUserService.selectreceptionUserByUserId(userId));
        ajaxResult.put("roleIds", receptionRoleService.selectreceptionRoleList(null));
        MenuUserRole menuUserRole = new MenuUserRole();
        menuUserRole.setUserId(userId);
        List<Long> collect = requestUserRoleService.selectMenuUserRoleList(menuUserRole).stream().map(m -> m.getRoleId()).collect(Collectors.toList());
        ajaxResult.put("roleId", collect);
        return ajaxResult;

    }

    /**
     * 新增用户信息
     */
    @PreAuthorize("@ss.hasPermi('system:receptionUser:add')")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReceptionUser receptionUser) {
        receptionUser.setCreateBy(getUsername());
        receptionUser.setPassword(SecurityUtils.encryptPassword("123456"));
        logger.info("数据源" + DynamicDataSourceContextHolder.getDataSourceType());
        receptionUserService.insertreceptionUser(receptionUser);
        DynamicDataSourceContextHolder.setDataSourceType(DynamicDataSourceContextHolder.getDataSourceType() == DataSourceType.MASTER.name() ? DataSourceType.SLAVE.name() : DataSourceType.MASTER.name());
        logger.info("数据源" + DynamicDataSourceContextHolder.getDataSourceType());
        return toAjax(receptionUserService.insertreceptionUser(receptionUser));
    }

    /**
     * 修改用户信息
     */
    @PreAuthorize("@ss.hasPermi('system:receptionUser:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReceptionUser receptionUser) {

        receptionUser.setUpdateBy(getUsername());
        if (receptionUser.getPassword() != null) {
            receptionUser.setPassword(SecurityUtils.encryptPassword(receptionUser.getPassword()));
        }

        receptionUserService.updatereceptionUser(receptionUser);
        logger.info("数据源" + DynamicDataSourceContextHolder.getDataSourceType());
        DynamicDataSourceContextHolder.setDataSourceType(DynamicDataSourceContextHolder.getDataSourceType() == DataSourceType.MASTER.name() ? DataSourceType.SLAVE.name() : DataSourceType.MASTER.name());
        logger.info("数据源" + DynamicDataSourceContextHolder.getDataSourceType());
        return toAjax(receptionUserService.updatereceptionUser(receptionUser));
    }

    /**
     * 删除用户信息
     */
    @PreAuthorize("@ss.hasPermi('system:receptionUser:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {

        logger.info("数据源" + DynamicDataSourceContextHolder.getDataSourceType());
        receptionUserService.deletereceptionUserByUserIds(userIds);
        DynamicDataSourceContextHolder.setDataSourceType(DynamicDataSourceContextHolder.getDataSourceType() == DataSourceType.MASTER.name() ? DataSourceType.SLAVE.name() : DataSourceType.MASTER.name());
        logger.info("数据源" + DynamicDataSourceContextHolder.getDataSourceType());
        return toAjax(receptionUserService.deletereceptionUserByUserIds(userIds));
    }
}
